package com.mainworks.commons.redis;

import com.mainworks.commons.constant.Constants;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Component;

import java.util.concurrent.TimeUnit;

/**
 * Redis工具类
 * @Author: HuangMinJie
 * @CreateDate: 13:59 2021/10/29
 * @Description:
 * @Version: 1.0.0.0
 * @ModifiedBy:
 * @ModifiedDate:
 * @ModifiedDescription:
 */
public class RedisUtils {

    public RedisTemplate<String,Object> redisTemplate;

    public RedisTemplate<String,String> redisTemplates;
    /**
     * 判断 Object key 是否存在
     *@Author: HuangMinJie
     *@param  key
     *@CreateDate:  2021/10/30 10:13
     *@Description:
     */
    public boolean hasKey(String key){
        try {
            return redisTemplate.hasKey(key);
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }
    }
    
    /**
     * 获取 Object 对现象值
     *@Author: HuangMinJie
     *@param  key
     *@CreateDate:  2021/10/30 10:20
     *@Description:
     */
    public Object getObject(String key) {
        return key == null? null : redisTemplate.opsForValue().get(key);
    }
    /**
     * 保存Object对象值
     *@Author: HuangMinJie
     *@param  key
     *@param  value
     *@CreateDate:  2021/10/30 10:22
     *@Description:
     */
    public boolean set(String key, Object value){
        try {
            redisTemplate.opsForValue().set(key,value);
            if (value ==null ||value.equals("")){
                redisTemplate.expire(key, Constants.Expire, TimeUnit.MILLISECONDS);
            }
            return true;
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }
    }
    /**
     * 保存有效的Object对象值
     *@Author: HuangMinJie
     *@param  key
     *@param  value
     *@param  time < 0 时默认是 Constants.Expire 这个参数
     *@CreateDate:  2021/10/30 10:28
     *@Description:
     */
    public boolean set(String key, Object value,long time){
        try {
            redisTemplate.opsForValue().set(key,value);
            if (time > 0) {
                redisTemplate.expire(key,time, TimeUnit.SECONDS);
            }else {
                redisTemplate.expire(key, Constants.Expire, TimeUnit.MILLISECONDS);
            }
            return true;
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }
    }

    public void deleteObject(String key)
    {
        redisTemplate.delete(key);
    }
    /**
     * 获取缓存名中的key
     *@Author: HuangMinJie
     *@param  cacheName
     *@param  key
     *@CreateDate:  2021/10/30 10:13
     *@Description:
     */
    public String getHash(String cacheName,String key)
    {
        return (String) redisTemplates.opsForHash().get(cacheName,key);
    }
    /**
     * 向缓存名中添加数据
     *@Author: HuangMinJie
     *@param  cacheName
     *@param  key
     *@param  value
     *@CreateDate:  2021/10/30 10:38
     *@Description:
     */
    public void put(String cacheName,String key,String value){
        redisTemplates.opsForHash().put(cacheName,key,value);
        if (value==null||"".equals(value)){
            RedisNullKeys.put(redisTemplates,cacheName+"_"+key);
        }
    }
    /**
     * 删除缓存名中的key
     *@Author: HuangMinJie
     *@param  cacheName
     *@param  key
     *@CreateDate:  2021/10/30 10:36
     *@Description:
     */
    public  void hashDelete(String cacheName,String key){
        redisTemplates.opsForHash().delete( cacheName, key );
    }

}
